package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.ManagementStatisAnalyseMapper;
import cc.rengu.igas.bsps.common.entity.PayTypeStatisInfo;
import cc.rengu.igas.bsps.common.entity.TransSumInfoAnalyse;
import cc.rengu.igas.bsps.common.enums.BspsMchntTypeEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 经营统计分析正式表接口类
 * Created by yuhongwen
 */
public class ManagementStatisAnalyseMapperImpl implements ManagementStatisAnalyseMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);

    @Override
    public List<TransSumInfoAnalyse> selectTransSunInfoList(String instId, String mchntNo, String startDate, String endDate, String statisType, String mchntType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTransSumInfoAnalyse");
        TransSumInfoAnalyse transSumInfoAnalyse = new TransSumInfoAnalyse();
        transSumInfoAnalyse.setInstId(instId);
        if (BspsMchntTypeEnum.GROUP_MCHNT.getMchntType().equals(mchntType)) {
            transSumInfoAnalyse.setGroupMchntNo(mchntNo);
        } else if (BspsMchntTypeEnum.PLAT_MCHNT.getMchntType().equals(mchntType)) {
            transSumInfoAnalyse.setPlatMchntNo(mchntNo);
        } else if (BspsMchntTypeEnum.AGENT_MCHNT.getMchntType().equals(mchntType)) {
            transSumInfoAnalyse.setAgentMchntNo(mchntNo);
        } else {
            transSumInfoAnalyse.setMchntNo(mchntNo);
        }
        transSumInfoAnalyse.setStartDate(startDate);
        transSumInfoAnalyse.setEndDate(endDate);
        transSumInfoAnalyse.setQueryType(statisType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, transSumInfoAnalyse, TransSumInfoAnalyse.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("交易统计信息查询失败");
            return null;
        }
        return objectList.stream().map(item -> (TransSumInfoAnalyse) item).collect(Collectors.toList());
    }

    @Override
    public List<PayTypeStatisInfo> selectPayTypeStatisInfoList(String instId, String mchntNo, String startDate, String endDate, String statisType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayTypeStatisInfo");
        PayTypeStatisInfo payTypeStatisInfo = new PayTypeStatisInfo();
        payTypeStatisInfo.setInstId(instId);
        payTypeStatisInfo.setMchntNo(mchntNo);
        payTypeStatisInfo.setStartDate(startDate);
        payTypeStatisInfo.setEndDate(endDate);
        payTypeStatisInfo.setQueryType(statisType);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payTypeStatisInfo, PayTypeStatisInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("支付方式统计信息查询失败");
            return null;
        }
        return objectList.stream().map(item -> (PayTypeStatisInfo) item).collect(Collectors.toList());
    }

    @Override
    public String selectPayTypeStatis(String instId, String mchntNo, String startDate, String endDate, String statisType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayTypeStatis");
        PayTypeStatisInfo payTypeStatisInfo = new PayTypeStatisInfo();
        payTypeStatisInfo.setInstId(instId);
        payTypeStatisInfo.setMchntNo(mchntNo);
        payTypeStatisInfo.setStartDate(startDate);
        payTypeStatisInfo.setEndDate(endDate);
        payTypeStatisInfo.setQueryType(statisType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, payTypeStatisInfo, payTypeStatisInfo);
        if (0 != iReturnCode) {
            rglog.error("获取行业类别信息失败,instId:<{}>,iReturnCode:<{}>", instId, iReturnCode);
            return null;
        }
        return payTypeStatisInfo.getPayTypeAmt();
    }

}
